home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / halma-12-c / Halma ƒ / Shell ƒ / buttons.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  6.2 KB  |  217 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        buttons.c
  4.  
  5. Purpose:    This module handles 3D buttons: drawing, clicking, tracking.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program in a file named "GNU General Public License".
  19. If not, write to the Free Software Foundation, 675 Mass Ave,
  20. Cambridge, MA 02139, USA.
  21.  
  22. \**********************************************************************/
  23.  
  24. #include "buttons.h"
  25.  
  26. static    RGBColor        gBlack4Bit={32768, 32768, 32768};
  27. static    RGBColor        gWhite4Bit={60948, 60948, 60948};
  28. static    RGBColor        gBackground4Bit={49152,49152,49152};
  29. static    RGBColor        gBlack8Bit={26214, 26214, 39322};
  30. static    RGBColor        gWhite8Bit={60948, 60948, 60948};
  31. static    RGBColor        gBackground8Bit={52429, 52429, 52429};
  32.  
  33. #define TRIANGLE_SIZE    6
  34.  
  35. void Draw3DButton(Rect *buttonRect, unsigned char *theTitle,
  36.     Handle iconHandle, short buttonDepth, Boolean isDown, Boolean drawTriangle)
  37. {
  38.     FontInfo        theFontInfo;
  39.     Rect            tempRect;
  40.     RGBColor        oldForeColor, oldBackColor;
  41.     PixPatHandle    backgroundppat;
  42.     short            theLineHeight;
  43.     Rect            iconRect;
  44.     RGBColor        myWhite={65535, 65535, 65535};
  45.     
  46.     TextFont(geneva);
  47.     TextSize(9);
  48.     TextFace(0);
  49.     GetFontInfo(&theFontInfo);
  50.     theLineHeight=theFontInfo.ascent+theFontInfo.descent+theFontInfo.leading;
  51.     if (iconHandle!=0L)
  52.     {
  53.         iconRect.left=(buttonRect->right-buttonRect->left)/2+buttonRect->left-16+
  54.             (isDown ? 1 : 0);
  55.         iconRect.right=iconRect.left+32;
  56.         iconRect.top=(buttonRect->bottom-buttonRect->top)/2+buttonRect->top-16+
  57.             (isDown ? 1 : 0)-((theTitle!=0L) ? 5 : 0);
  58.         iconRect.bottom=iconRect.top+32;
  59.     }
  60.     
  61.     if (buttonDepth>2)
  62.     {
  63.         GetForeColor(&oldForeColor);
  64.         GetBackColor(&oldBackColor);
  65.         
  66.         backgroundppat=NewPixPat();
  67.         MakeRGBPat(backgroundppat, (buttonDepth==4) ? &gBackground4Bit :
  68.             &gBackground8Bit);
  69.         SetRect(&tempRect, buttonRect->left+2, buttonRect->top+2,
  70.             buttonRect->right-2, buttonRect->bottom-2);
  71.         
  72.         FillCRect(&tempRect, backgroundppat);
  73.  
  74.         RGBBackColor(&myWhite);
  75.         RGBForeColor(isDown ? ((buttonDepth==4) ? &gBlack4Bit : &gBlack8Bit) :
  76.             ((buttonDepth==4) ? &gWhite4Bit : &gWhite8Bit));
  77.         MoveTo(buttonRect->left, buttonRect->top);
  78.         LineTo(buttonRect->right-2, buttonRect->top);
  79.         MoveTo(buttonRect->left, buttonRect->top+1);
  80.         LineTo(buttonRect->right-3, buttonRect->top+1);
  81.         MoveTo(buttonRect->left, buttonRect->top+2);
  82.         LineTo(buttonRect->left, buttonRect->bottom-2);
  83.         MoveTo(buttonRect->left+1, buttonRect->top+2);
  84.         LineTo(buttonRect->left+1, buttonRect->bottom-3);
  85.         
  86.         RGBForeColor(isDown ? ((buttonDepth==4) ? &gWhite4Bit : &gWhite8Bit) :
  87.             ((buttonDepth==4) ? &gBlack4Bit : &gBlack8Bit));
  88.         MoveTo(buttonRect->right-1, buttonRect->top);
  89.         LineTo(buttonRect->right-1, buttonRect->bottom-1);
  90.         MoveTo(buttonRect->right-2, buttonRect->top+1);
  91.         LineTo(buttonRect->right-2, buttonRect->bottom-1);
  92.         MoveTo(buttonRect->left+2, buttonRect->bottom-2);
  93.         LineTo(buttonRect->right-1, buttonRect->bottom-2);
  94.         MoveTo(buttonRect->left+1, buttonRect->bottom-1);
  95.         LineTo(buttonRect->right-1, buttonRect->bottom-1);
  96.     }
  97.     else
  98.     {
  99.         EraseRect(buttonRect);
  100.         FrameRect(buttonRect);
  101.         if (!isDown)
  102.         {
  103.             MoveTo(buttonRect->left+2, buttonRect->bottom-2);
  104.             LineTo(buttonRect->right-2, buttonRect->bottom-2);
  105.             LineTo(buttonRect->right-2, buttonRect->top+2);
  106.         }
  107.         else
  108.         {
  109.             MoveTo(buttonRect->left+1, buttonRect->bottom-3);
  110.             LineTo(buttonRect->left+1, buttonRect->top+1);
  111.             LineTo(buttonRect->right-3, buttonRect->top+1);
  112.         }
  113.     }
  114.     
  115.     if (theTitle!=0L)
  116.     {
  117.         ForeColor(blackColor);
  118.         MoveTo(buttonRect->left+(buttonRect->right-buttonRect->left)/2-
  119.             (StringWidth(theTitle)/2)+(isDown ? 1 : 0), ((iconHandle!=0L) ?
  120.             buttonRect->bottom-6 : buttonRect->top+
  121.             (buttonRect->bottom-buttonRect->top)/2+theLineHeight/2-2)+
  122.             (isDown ? 1 : 0));
  123.         DrawString(theTitle);
  124.     }
  125.     
  126.     if (iconHandle!=0L)
  127.     {
  128.         if (buttonDepth>2)
  129.             PlotCIcon(&iconRect, (CIconHandle)iconHandle);
  130.         else
  131.         {
  132.             BitMap    iconMap;
  133.             GrafPtr curPort;
  134.             
  135.             GetPort(&curPort);
  136.             HLock( iconHandle);    /* lock data in place */
  137.             
  138.             iconMap.baseAddr = *iconHandle;    /* dereference the handle */
  139.             iconMap.rowBytes = 4;            /* setup other fields */
  140.             SetRect( &iconMap.bounds, 0,0,32,32);
  141.             
  142.             CopyBits( &iconMap, &(curPort->portBits),
  143.                          &iconMap.bounds, &iconRect, srcOr, 0L );
  144.             
  145.             HUnlock(iconHandle);                /* all done; let it float */
  146.         }
  147.     }
  148.     
  149.     if (drawTriangle)
  150.     {
  151.         RgnHandle        triangleRgn;
  152.         
  153.         triangleRgn=NewRgn();
  154.         OpenRgn();
  155.             MoveTo(buttonRect->right-6+(isDown ? 1 : 0), buttonRect->top+5+(isDown ? 1 : 0));
  156.             Line(-TRIANGLE_SIZE, TRIANGLE_SIZE);
  157.             Line(-TRIANGLE_SIZE, -TRIANGLE_SIZE);
  158.             Line(TRIANGLE_SIZE*2, 0);
  159.         CloseRgn(triangleRgn);
  160.         FillRgn(triangleRgn, black);
  161.         DisposeRgn(triangleRgn);
  162.     }
  163.     
  164.     if (buttonDepth>2)
  165.     {
  166.         DisposPixPat(backgroundppat);
  167.         RGBForeColor(&oldForeColor);
  168.         RGBBackColor(&oldBackColor);    
  169.     }
  170. }
  171.  
  172. Boolean Track3DButton(Rect *buttonRect, unsigned char *theTitle,
  173.     Handle iconHandle, short buttonDepth, Boolean drawTriangle)
  174. {
  175.     Point            mouseLoc;
  176.     Boolean            buttonIsDown;
  177.     
  178.     buttonIsDown=FALSE;
  179.     while (StillDown())
  180.     {
  181.         GetMouse(&mouseLoc);
  182.         if (PtInRect(mouseLoc, buttonRect))
  183.         {
  184.             if (!buttonIsDown)
  185.             {
  186.                 buttonIsDown=TRUE;
  187.                 Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth,
  188.                     TRUE, drawTriangle);
  189.             }
  190.         }
  191.         else
  192.         {
  193.             if (buttonIsDown)
  194.             {
  195.                 buttonIsDown=FALSE;
  196.                 Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth,
  197.                     FALSE, drawTriangle);
  198.             }
  199.         }
  200.     }
  201.     
  202.     if (buttonIsDown)
  203.         Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth, FALSE, drawTriangle);
  204.     
  205.     return buttonIsDown;
  206. }
  207.  
  208. void Hit3DButton(Rect *buttonRect, unsigned char *theTitle,
  209.     Handle iconHandle, short buttonDepth, Boolean drawTriangle)
  210. {
  211.     long            dummy;
  212.     
  213.     Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth, TRUE, drawTriangle);
  214.     Delay(8, &dummy);
  215.     Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth, FALSE, drawTriangle);
  216. }
  217.